﻿using System;
using System.Collections.Generic;
using System.Text;
using Mang24k99.Model.PUBC;
using NHibernate;
using NHibernate.Expression;
using Castle.ActiveRecord.Queries;
using Mang24k99.Model.COMM;
namespace Mang24k99.Business.PUBC
{
    public partial class BKtPubcCyc
    {
        public static void Create(MKtPubcCyc model)
        {
            model.IsValidate = 1;
            model.InputTime = DateTime.Now;
            model.ModifyTime = DateTime.Now;
            model.DeleteTime = DateTime.Now;
            model.Create();
        }
        public static void Update(MKtPubcCyc model)
        {
            model.ModifyTime = DateTime.Now;
            model.Update();
        }
        public static void Delete(MKtPubcCyc model)
        {
            //删除默认字段在ondelete中执行
            model.Delete();
        }

        public static IList<MKtPubcCyc> FindAll()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcCyc.SelectAll(criteria);
        }

        public static MKtPubcCyc Find(object ID)
        {
            return MKtPubcCyc.SelectByPrimaryKey(ID);
        }
        public static MKtPubcCyc FindOne()
        {
            ICriterion[] criteria = new ICriterion[]
            {
            };
            return MKtPubcCyc.SelectOne(criteria);
        }
        public static MKtPubcCyc FindFirst()
        {
            ICriterion[] criteria = new ICriterion[]
            {
            };
            return MKtPubcCyc.SelectFirst(criteria);
        }

        public static IList<MKtPubcCyc> FindALLByProperty(string property, object value)
        {
            return MKtPubcCyc.SelectALLByProperty(property, value);
        }

        public static IList<MKtPubcCyc> FindALLSliced(int firstResult, int maxResult)
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcCyc.SelectALLSliced(firstResult, maxResult, criteria);
        }
        public static IList<MKtPubcCyc> FindALLQuery(int firstResult, int maxResult)
        {
            string strHql = @"Select MKtPubcCyc"
                          + " from"
                          + " MKtPubcCyc MKtPubcCyc, MKtPubcProduct MKtPubcProduct, MKtCommCodeList MKtCommCodeList"
                          + " where"
                          + " MKtPubcCyc.IsValidate = 1"
                          + " and MKtPubcCyc.MKtPubcProduct.BoardNO = MKtPubcProduct.BoardNO"
                          + " and MKtPubcCyc.MKtCommCodeList.CODE_NO = MKtCommCodeList.CODE_NO"
                          ;
            SimpleQuery query = new SimpleQuery(typeof(MKtPubcCyc), strHql);
            query.SetQueryRange(firstResult, maxResult);
            return MKtPubcCyc.SelectAllQuery(query);
        }
        public static int Count()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IsValidate",1),
            };
            return MKtPubcCyc.Counts(criteria);
        }
        public static bool Exists(object id)
        {
            return MKtPubcCyc.Exist(id);
        }
        /// <summary>
        /// 选择周期类型
        /// </summary>
        /// <returns></returns>
        public static IList<MKtCommCodeList> SelectCycType()
        {
            MKtCommCodeList codelist = new MKtCommCodeList();
            string where = " where codelist.MKtCommCodeType.TYPE_CODE=309 and codelist.IS_VALIDATE=1 ";
            return (IList<MKtCommCodeList>)MKtCommCodeList.SelectByWhere(where);
        }
        /// <summary>
        /// 根据看板编号以及周期名称查询记录
        /// </summary>
        /// <param name="boardno"></param>
        /// <returns></returns>
        public static IList<MKtPubcCyc> NameValidate(int boardno,string cycname)
        {
            MKtPubcCyc cyc = new MKtPubcCyc(); 
            string where = " where cyc.MKtPubcProduct.BoardNO="+boardno;
            where += " and cyc.CycName='"+cycname+"'";
            return (IList<MKtPubcCyc>)MKtPubcCyc.SelectByWhere(where);
        }
        /// <summary>
        /// 根据看板编号以及周期简称查询记录
        /// </summary>
        /// <param name="boardno"></param>
        /// <param name="cycalias"></param>
        /// <returns></returns>
        public static IList<MKtPubcCyc>AliasValidate(int boardno,string cycalias)
        {
            string where = " where cyc.MKtPubcProduct.BoardNO=" + boardno;
            where += " and cyc.CycAlias='" + cycalias + "'";
            return (IList<MKtPubcCyc>)MKtPubcCyc.SelectByWhere(where);
        }
        /// <summary>
        /// 根据看板编号查询记录
        /// </summary>
        /// <param name="boardno"></param>
        /// <returns></returns>
        public static IList<MKtPubcCyc> SelectByBoardNO(int boardno)
        {
            string where = " where cyc.MKtPubcProduct.BoardNO=" + boardno;
            where += " and cyc.IsValidate=1";
            where += " and cyc.MKtCommCodelist.IS_VALIDATE=1";
            return (IList<MKtPubcCyc>)MKtPubcCyc.SelectByWhere(where);
        }
        /// <summary>
        /// 根据看板编号及周期类型查询记录
        /// </summary>
        /// <param name="boardno"></param>
        /// <param name="cycno"></param>
        /// <returns></returns>
        public static IList<MKtPubcCyc> CycValidate(int boardno, int cyctype)
        {
            MKtPubcCyc cyc = new MKtPubcCyc();
            string where = " where cyc.MKtPubcProduct.BoardNO=" + boardno;
            where += " and cyc.MKtCommCodelist.CODE_NO=" +cyctype;
            return (IList<MKtPubcCyc>)MKtPubcCyc.SelectByWhere(where);
        }
    }
}
